package com.isccp.efi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isccp.efi.dto.*;
import com.isccp.efi.mapper.B01301Mapper;
import com.isccp.efi.service.B013Service;
import com.isccp.efi.mapper.B013Mapper;
import com.isccp.efi.util.GetDataUtil;
import com.thoughtworks.xstream.XStream;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author songyong
* @description 针对表【b_013(b_013 同步包装卡)】的数据库操作Service实现
* @createDate 2023-12-25 17:00:40
*/
@Service
public class B013ServiceImpl extends ServiceImpl<B013Mapper, B013>
    implements B013Service{
    private static final Logger log = LoggerFactory.getLogger(PackagecardServiceImpl.class);
    @Resource
    private B01301Mapper b01301Mapper;

    @Resource
    private  GetDataUtil getDataUtil;

    private String saveData(List<SynPackingCard> arr, String dprtCode){
        QueryWrapper queryWrapper=  new QueryWrapper<B013>().eq("bzdyh",arr.get(0).getBzh());
        if(this.getBaseMapper().selectCount(queryWrapper)>0) {
            return arr.get(0).getBzh();
        }
        SynPackingCardXml synPackingCardXml=new SynPackingCardXml();
        BeanUtils.copyProperties(arr.get(0),synPackingCardXml);
        String time= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        List<SynPart> list=new ArrayList<>();
        SynPackingCard s0=arr.get(0);
        B013 b013=new B013();
        BeanUtils.copyProperties(s0,b013);
        b013.setBzdyh(s0.getBzh());
        b013.setCjsj(new Date());
        b013.setCsbj(0);
        b013.setJsbj(0);
        b013.setZt("1");
        b013.setLlzt("0");
        b013.setGysbm(s0.getCysbm());
        b013.setDprtcode("");
        b013.setDprtcode(dprtCode);
        b013.setUaxh(s0.getUatype());
        this.save(b013);
        for (SynPackingCard s : arr) {
            B01301 b01301=new B01301();
            BeanUtils.copyProperties(s,b01301);
            b01301.setBzdyh(s.getBzh());
            b01301.setZdid(b013.getId());
            b01301.setCavalue(s.getCvalue());
            b01301.setWz(s.getSite());
            b01301.setCClass(s.getCclass());
            try {
                b01301.setDyxh(s.getXh());
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            b01301Mapper.insert(b01301);
        }
        return arr.get(0).getBzh();
    }


    private List<List<SynPackingCard>> groupList(List<SynPackingCard> list){
        List<List<SynPackingCard>> res=new ArrayList<>();
        String prev="";

        for(SynPackingCard synPackingCard:list){
            if(prev.equals(synPackingCard.getBzh())){
                res.get(res.size()-1).add(synPackingCard);
            }
            else {
                List<SynPackingCard> arr=new ArrayList<>();
                arr.add(synPackingCard);
                res.add(arr);
                prev=synPackingCard.getBzh();
            }
        }
        return res;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public   void getData(String uc,String gysbm,String password,String dprtCode) {
        log.info(uc+":"+gysbm+"-------开始请求数据------");
        String repStr= getDataUtil.synPackingCardService(uc,gysbm,password);
        String contentStr = repStr.substring(repStr.indexOf("<WsResult>"), repStr.indexOf("</WsResult>") + 11);
        XStream stream = new XStream();
        stream.alias("WsResult", WsResultDto.class);
        stream.alias("SynPackingCard", SynPackingCard.class);
        WsResultDto wsResultDto = (WsResultDto)stream.fromXML(contentStr);
        if (!"true".equals(wsResultDto.getIsSuccess())) {
        } else {
            if(wsResultDto.getList().size()>0){
                List<List<SynPackingCard>> list = groupList(wsResultDto.getList());
                List<String> recbzh=new ArrayList<>();
                for(List<SynPackingCard> arr:list) {
                    recbzh.add(saveData(arr,dprtCode));
                }
                StringBuffer bzhStr = new StringBuffer();
                for(int i = 0; i < recbzh.size(); ++i) {
                    if (i > 0) {
                        bzhStr.append(",");
                    }
                    bzhStr.append((String)recbzh.get(i));
                }
                log.info(uc+":"+gysbm+"获取到数据"+bzhStr.toString());
                getDataUtil.dataRecService("1", bzhStr.toString());

            }
        }
        log.info(uc+":"+gysbm+"------- 请求数据完成---------");
    }

}




